package com.jiazhong.basic.shuxin.thread.c;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class App {

    private static void a() {
        // 单线程池
        ExecutorService executor = Executors.newSingleThreadExecutor();
        // 四个任务
        DemoA d1 = new DemoA();
        DemoA d2 = new DemoA();
        DemoA d3 = new DemoA();
        DemoA d4 = new DemoA();
        // 将四个任务交由线程池执行
        executor.execute(d1);
        executor.execute(d2);
        executor.execute(d3);
        executor.execute(d4);
        executor.shutdown();
    }

    private static void b() {
        // 定长线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);
        // 四个任务
        DemoA d1 = new DemoA();
        DemoA d2 = new DemoA();
        DemoA d3 = new DemoA();
        DemoA d4 = new DemoA();
        // 将四个任务交由线程池执行
        executor.execute(d1);
        executor.execute(d2);
        executor.execute(d3);
        executor.execute(d4);
        executor.shutdown();
    }

    private static void c() {
        // 可变线程池
        ExecutorService executor = Executors.newCachedThreadPool();
        // 四个任务
        DemoA d1 = new DemoA();
        DemoA d2 = new DemoA();
        DemoA d3 = new DemoA();
        DemoA d4 = new DemoA();
        // 将四个任务交由线程池执行
        executor.submit(d1);
        executor.execute(d2);
        executor.execute(d3);
        executor.execute(d4);
        executor.shutdown();
    }

    private static void d() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        DemoB b = new DemoB();
        // scheduledExecutorService.schedule(b, 10, TimeUnit.SECONDS);
        scheduledExecutorService.scheduleAtFixedRate(b, 10, 2, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        c();
    }
}
